<?php

namespace App\Console\Commands;

use DB;
use Illuminate\Console\Command;

class tools extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tools {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'tools article | tools imgs | tools batch';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $action = $this->argument('action');
        if($action == 'article')
        {
            $this->article();
        }
        if($action == 'imgs')
        {
            $this->imgs();
        }
        if($action == 'batch')
        {
        	$this->batch();
        }
        if($action == 'crontabs')
        {
        	$this->crontabs();
        }
    }

    public function article()
    {
        $day    = date('Y-m-d H:i:s', time());
        $count  = DB::connection('webshowu')->table('articles')->count();
        $num    = ceil($count/100);
        for($i=0;$i<$num;$i++)
        {
            $art = DB::connection('webshowu')->table('articles')->skip($i*100)->take(100)->get();
            foreach ($art as $v)
            {
                $column_id  = 0;
                switch($v->cate_id)
                {
                    case 17:
                        $column_id = 148;
                        break;
                    case 18:
                        $column_id = 149;
                        break;
                    case 19:
                        $column_id = 150;
                        break;
                    case 20:
                        $column_id = 151;
                        break;
                    case 21:
                        $column_id = 152;
                        break;
                    case 48:
                        $column_id = 153;
                        break;
                    case 72:
                        $column_id = 154;
                        break;
                    case 73:
                        $column_id = 155;
                        break;
                    case 74:
                        $column_id = 156;
                        break;
                    case 75:
                        $column_id = 157;
                        break;
                    case 76:
                        $column_id = 158;
                        break;
                    case 77:
                        $column_id = 159;
                        break;
                }

                DB::table('articles')->insert(
                    ['column_id'=>$column_id,'title'=>$v->art_title,'copy_from'=>$v->copy_from.'|'.$v->copy_url,'keywords'=>$v->art_tags,'description'=>$v->art_intro,'content'=>$v->art_content,'views'=>$v->art_views,'state'=>1,'created_at'=>$day,'updated_at'=>$day]
                );
                $this->line($v->art_id);
            }
        }

    }
    
    public function imgs()
    {
        $count  = DB::table('articles')->count();
        $num    = ceil($count/100);
        for($i=0;$i<$num;$i++)
        {
            $art = DB::table('articles')->skip($i*100)->take(100)->get();
            foreach ($art as $v)
            {
            	$content	= str_replace('http://img.webshowu.com/','https://img.dmozhub.com/',$v->content);
            	DB::table('articles')->where('id', $v->id)->update([
            		'content' => $content
            		]);
                $this->line($v->id);
            }
        }

    }
    
    public function batch()
    {
    	$count  = DB::table('sites')->count();
        $num    = ceil($count/50);
        $this->line($num.'=='.$count);
        for($i=0;$i<$num;$i++)
        {
        	$urls = [];
            $art = DB::table('sites')->where('state', 1)->skip($i*50)->take(50)->get();
            foreach ($art as $v)
            {
				$urls[] = env('APP_URL').'/siteinfo-'.$v->id.'.html';
            }
			$api = 'http://data.zz.baidu.com/urls?appid=1585865613834752&token=m6dYw2sRB4Isde2N&type=batch';
			$ch = curl_init();
			$options =  array(
			    CURLOPT_URL => $api,
			    CURLOPT_POST => true,
			    CURLOPT_RETURNTRANSFER => true,
			    CURLOPT_POSTFIELDS => implode("\n", $urls),
			    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
			);
			curl_setopt_array($ch, $options);
			$result = curl_exec($ch);
			$this->line($i);
        }
    }
    
    public function crontabs()
    {
    	$count  = DB::table('crontabs')->count();
        $num    = ceil($count/50);
        $this->line($num.'=='.$count);
        for($i=0;$i<$num;$i++)
        {
            $art = DB::table('crontabs')->skip($i*50)->take(50)->get();
            foreach ($art as $v)
            {
            	//$this->line($v->id.'=='.$schedule);
            	$contents	= str_replace("www.1qnn.com","42.51.1.120", $v->contents);
            	DB::table('crontabs')->where('id', $v->id)->update(['contents' => $contents]);
            }
			$this->line($i);
        }
    }
}
